package com.demo2do.jit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.EditText;

import com.demo2do.jit.remote.ApiSubscriber;
import com.demo2do.jit.remote.Result;
import com.demo2do.jit.remote.api.JitApi;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Headers;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {

    @Inject
    JitApi jitApi;

    @Inject
    JitApplication jitApplication;

    @BindView(R.id.username)
    EditText usernameEditText;

    @BindView(R.id.password)
    EditText passwordEditText;

    /** 设备编号 */
    private String scanid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getActivityComponent().inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        // Get android device id
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        scanid = telephonyManager.getDeviceId();

    }

    @OnClick(R.id.login)
    void login() {

        String username = usernameEditText.getText().toString();
        String password = passwordEditText.getText().toString();

        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            toastShort("用户名密码不能为空");
            return;
        }

        jitApi.login(username, password, scanid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showLoading("登录中");
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        hideLoading();
                    }
                })
                .subscribe(new ApiSubscriber<Result>(this) {

                    @Override
                    protected void httpSuccess(Result result) {
                        System.out.println(result);
                        if (result.isValid()) {

                            // get cookie from headers
                            Headers headers = returnHeaders();
                            String cookieVal = headers.get("Set-Cookie");
                            String cookie = cookieVal.substring(0, cookieVal.indexOf(";"));

                            // save cookie to application
                            jitApplication.setCookie(cookie);

                            Intent intent = new Intent(LoginActivity.this, GasActivity.class);

                            // pass fullName to GasActivity
                            String fullName = (String) result.getObj().get("fullName");
                            intent.putExtra("fullName", fullName);

                            startActivity(intent);

                            // clear input field
                            final Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                usernameEditText.setText("");
                                passwordEditText.setText("");
                                }
                            }, 100);

                        } else {
                            toastShort(result.getMsg());
                        }
                    }

                });
    }


}
